<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="java.util.ArrayList,
                 java.util.Iterator,
                 java.util.HashMap,
                 org.apache.struts.util.LabelValueBean,
                 com.scalar.chiptrack.users.dao.UserDAO,
                 com.scalar.chiptrack.users.UserGroup,
                 com.scalar.chiptrack.users.Level,
                 com.scalar.chiptrack.users.LevelAccess,
                 com.scalar.chiptrack.users.Functionality,
                 com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.commons.Constants"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/jsp/admin/access_level_setup.jsp"/>
<html:html locale="true">

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>

<script language='javascript'>
function loadLevel () {
    formAction = "<%=chipTracContext%>/jsp/admin/access_level_setup.jsp?level=" + document.accessLevelSetupForm.levelId.value
    document.accessLevelSetupForm.action=formAction;
    document.accessLevelSetupForm.submit();
}
var enableFlag=true;
var errors ="";
function saveLevel() {

		 if( (document.accessLevelSetupForm.levelId.value == -1 )  )
		{
			errors = '';
			errors = errors + '<br>' + "<li>  Select Access Level Setup ";

			if( enableFlag == false )
            {
				 if( document.accessLevelSetupForm.levelName.value != '')
				{
				   errors = '';
				}
				else if( document.accessLevelSetupForm.levelName.value == '')
				{
				   errors = '';
				   errors = errors + '<br>' + "<li>  Either select or enter Access Level Setup ";
				}

			}
	    }

		if( (document.accessLevelSetupForm.levelId.value != -1 )  )
		{
			errors = '';
			//errors = errors + '<br>' + "<li>  Select Access Level Setup ";

			if( enableFlag == false )
            {
				 if( document.accessLevelSetupForm.levelName.value != '')
				{
				   errors = '';
				}
				else if( document.accessLevelSetupForm.levelName.value == '')
				{
				   errors = '';
				   //errors = errors + '<br>' + "<li>   You can enter New Access Level Setup or Use the selected Value ";
				}

			}
	    }

     //alert(errors);
	  if( errors != '')
	   {
	  	    var pa = document.getElementById("errors");
			pa.innerHTML = errors;
			return;
	   }

     else
	{
      document.accessLevelSetupForm.submit();
	}
}
</script>

<SCRIPT>

function newLevel() {
    if (enableFlag == true) {
        var htmltext1 = '<font class="NArialL">Enter Level Name:</font>&nbsp;&nbsp;'
        var htmltext2 = '<input type="text" name="levelName" size="25" maxlength="25" class="txfield">'
        var pa1=document.getElementById("font1");
        var pa2=document.getElementById("font2");
        pa1.insertAdjacentHTML('BeforeEnd',htmltext1);
        pa2.insertAdjacentHTML('BeforeEnd',htmltext2);
        enableFlag = false;
    }
}
</SCRIPT>

<style>
<!--
a.nav:link   { font-family: Verdana,Arial,San Serif; font-size: 10px; text-decoration: none }
a.nav:visited { font-family: Verdana,Arial,San Serif; font-size: 10px; text-decoration: none }
a.nav:active { font-family: Verdana,Arial,San Serif; font-size: 10px; text-decoration: none }
a.nav:hover  { font-family: Verdana,Arial,San Serif; font-size: 10px; text-decoration: none; color: #000000 }
.HEADING     { cursor: hand; font-family: Verdana,Arial,San Serif; font-size: 12px; color: #000000;
               background-color: #FFFFFFF; font-weight: none;
               border: 1 solid #000000 }
.LINKSOFF    { display: none; font-family: Verdana,Arial,San Serif; font-size: 12px; color: #000000; }
.LINKSON     { display: inline; font-family: Verdana,Arial,San Serif; font-size: 12px; color: #000000; }
-->
</style>

<Script Language="VBscript">
// Copyright 2001 Stephen Hayes (steve@obtsolutions.co.uk)
// OBT Solutions UK (http://www.obtsolutions.co.uk)
// Permission granted to SimplytheBest.net to feature script in its
// DHTML script collection at http://simplythebest.net/info/dhtml_scripts.html
<!--
Option Explicit
DIM menuID,menuObj,cellBk
SUB ShowHide(menuID)
menuObj = "Link" & menuID
cellbk = "Head" & menuID
// CHECK IF OPEN
if document.all.item(menuObj).className = "LINKSON" then    'IF MENU OPEN THEN CLOSE
document.all.item(menuObj).className = "LINKSOFF"
document.all.item(cellBk).style.backgroundColor = "#FFFFFF"   ' SET CELL BACKGROUND COLOUR BLUE
ELSE
document.all.item(menuObj).className = "LINKSON"               ' OPEN MENU
document.all.item(cellBk).style.backgroundColor = "#FFFFFF"  ' SET CELL BACKGROUND COLOUR RED
END IF
END SUB
-->
</Script>


<META content="MSHTML 5.50.4807.2300" name=GENERATOR></HEAD>
<BODY bgColor="#ffffff" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0"> <!--onload="toggleBox('textDiv1',0);toggleBox('textDiv2',0);"-->

<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
  <TR>
	<td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

<!-- Side Navigation start -->
<%@ include file="/jsp/admin/admin_menu.jsp" %>
<!-- end of the side navigations -->
</TD>

<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>

<%      // Load All User groups from database

ArrayList levelList = UserDAO.loadAllLevels();
if (levelList == null) {
    levelList = new ArrayList();
}

int levelIdFromRequest = -1;
try {
    levelIdFromRequest = Integer.parseInt (request.getParameter ("level"));
}
catch (Exception ignore) {
    // no level parameter.
}

ArrayList levelAccessList = UserDAO.loadLevelAccessListByLevelId (levelIdFromRequest);
HashMap viewAccessMap = new HashMap();
HashMap modifyAccessMap = new HashMap();
HashMap deleteAccessMap = new HashMap();

if (levelAccessList != null) {

    int size = levelAccessList.size();
    LevelAccess levelAccess = null;

    for (int i=0; i<size; i++) {
        levelAccess = (LevelAccess) levelAccessList.get (i);
        viewAccessMap.put (levelAccess.getFunctionId()+"", ""+levelAccess.getView());
        modifyAccessMap.put (levelAccess.getFunctionId()+"", ""+levelAccess.getModify());
        deleteAccessMap.put (levelAccess.getFunctionId()+"", ""+levelAccess.getDelete());
    }
}

ArrayList userGroups = UserDAO.loadAllUserGroups();
String[] userGroupNames = null;
ArrayList[] functionsList = null;

if (userGroups != null) {

    int size = userGroups.size();
    userGroupNames = new String[size];
    functionsList = new ArrayList[size];
    UserGroup userGroup = null;

    for (int i=0; i<size; i++) {
        userGroup = (UserGroup) userGroups.get(i);
        userGroupNames[i] = userGroup.getName();
        functionsList[i] = UserDAO.loadFunctionsByGroupID (userGroup.getId());
    }
}
if ( user != null )
{
    if ( user.isActionAllowed ( Functionality.ACCESS_LEVEL_SETUP, User.USER_ACTION_MODIFY ) )
    {
        request.setAttribute ( "showMessage", "FALSE" );
    }
    else
    {
        request.setAttribute ( "showMessage", "TRUE" );
    }
}
%>



<html:form name="accessLevelSetupForm" type="com.scalar.chiptrack.users.forms.AccessLevelSetupForm" action="/accessLevelSetup">

<center><span class="NArialW">Access Level Setup</span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20>


<table border="0" width="90%" bordercolor="#FFFFFF" align=right>
<tr><td colspan ='2' align="left" class='NArialL'><font styleClass="NArialL" id="errors" color="red"  />&nbsp;</font></td></tr>
<tr><td colspan ='2' align="left" class='NArialL'><font styleClass="NArialL" id="message2" color="red"  />&nbsp;</font></td></tr>

    <tr><td colspan='2'><chiptrack:info styleClass='NArialL'/></td></tr>
    <tr><td colspan='2'><chiptrack:errors styleClass='NArialL'/></td></tr>
    <logic:equal name="showMessage" value="TRUE">
        <tr><td colspan=2 align='left'>
        <font class='NArialS'>
        <font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b>
        </font></td></tr>
    </logic:equal>
    <tr><td colspan='2'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>

<tr>

    <td width='20%' align="left" nowrap>
    <font class="NArialL">Access Level:&nbsp;&nbsp;</font>
    </td>
    <td align="left">
            <select class='NArialL' name="levelId" onchange="loadLevel('levelId')">
            <option value="-1">--Select Access Level --</option>
            <%
                int levelListSize = levelList.size();
                Level level = null;
                for (int i=0; i<levelListSize; i++) {
                    level = (Level) levelList.get (i);
                    out.print ("<option value=" + level.getId());
                    if (levelIdFromRequest == level.getId()) out.print (" selected");
                    out.println (" > " + level.getName());
                }
            %>
            </select>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <chiptrack:restrictedLink functionality='<%=Functionality.ACCESS_LEVEL_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:newLevel();' onmouseover="window.status='Click to Add New Access Level';return true" onmouseout="window.status='';return true">
                <img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" title='Click here to Add New Access Level'/>
            </chiptrack:restrictedLink>
            <%--<a href="javascript:newLevel()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" width="65" height="17" onMouseOver="window.status='Click to Add New Access Level';return true" onMouseout="window.status='';return true" title="Click here to Add New Access Level"></a>--%>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <chiptrack:restrictedLink functionality='<%=Functionality.ACCESS_LEVEL_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:saveLevel();' onmouseover="window.status='Click to Save Access Level';return true" onmouseout="window.status='';return true">
                <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" title='Click here to Save Access Level'/>
            </chiptrack:restrictedLink>
            <%--<a href="javascript:saveLevel()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onMouseOver="window.status='Click to Save Access Level';return true" onMouseout="window.status='';return true" title="Click here to Save Access Level" ></a>--%>
    </td>
</tr>

<tr>
    <td align="left">
        <font id="font1"></font>    <% //<font class="NArialL">Level Name</font>&nbsp;&nbsp;' %>
    </td>
    <td align="left">
        <font id="font2"></font>    <% // <input type="text" name="levelName" size="25" maxlength="25"> %>
    </td>
<tr>

<tr><td colspan=2><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=5></td></tr>
<tr>
<td align='right' valign='center' width='3%'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/exclamation-46x47.gif" width="24" height="24" align='center'></td>
<td align='left' valign='center' nowrap><font class="NArialS">Click on 'Add more' to add new Access Level</font></td>
</tr>
<tr><td colspan=2><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=5></td></tr>

<tr><td colspan=2>


<!-- GROUPS AND FUNCTIONALITY DISPLAY CODE STARTS HERE -->
<table align="left" border="0" width="90%">

<%
if (userGroups != null) {

    int userGroupSize = userGroups.size();
    String headValue = null;
    String linkValue = null;
    String showHide = null;

    // Write the table row for each user group
    for (int index=0; index<userGroupSize; index++) {

        headValue = "head" + index;             //
        linkValue = "link" + index;             // Values used by the script
        showHide = "ShowHide(" + index + ")";   //

        // We have user group name and corresponding permissions
        // Generate equivalent HTML Code to display this user group
%>

<tr>
<td ID='<%=headValue%>' ALIGN='left' width="113" height="15" CLASS="HEADING" ONCLICK='<%=showHide%>'><%=userGroupNames[index]%></td>
</tr>
<tr>
<td ALIGN="left">
<div ID='<%=linkValue%>' CLASS="LINKSOFF">
        <table border="0" width="100%" bordercolor="#FFFFFF">
            <tr>
                <td width="33%" align="center" bgcolor="#993333">
                    <p align="center"><font class="NArialL"><font color="#FFFFFF">Functionality</font></p>
                </td>
                <td width="66%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Actions</font></td>
	  </tr>
<%
        // Print out HTML Equivalent to display each functionality name
        // and its corresponding actions
        Functionality functionality = null;
        int functionNamesSize = functionsList[index].size();
        String functionName = null;
        int functionId = -1;
        int groupId = -1;
        for (int j=0; j<functionNamesSize; j++) {
            functionality = (Functionality) functionsList[index].get(j);
            functionName = functionality.getName();
            functionId = functionality.getId();
            groupId = functionality.getGroupId();

%>
            <tr>
                <td width="33%" align="center" <%if (j%2 == 0) out.print ("bgcolor='#DEE1E2'");%>><font class="NArialL"><%=functionName%></font></td>
                <td width="66%" align="left" <%if (j%2 == 0) out.print ("bgcolor='#DEE1E2'");%>><font class="NArialL">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <%if (functionality.getView() == true) {%>
                    <input type='checkbox' <%if ("true".equalsIgnoreCase ((String) viewAccessMap.get (functionId+""))) out.print("checked");%> name='<%="FUNCTION_ID," + functionId + "," + groupId + ",view"%>'> View &nbsp;&nbsp;&nbsp;&nbsp;
                <%}%>

                <%if (functionality.getModify() == true) {%>
                    <input type='checkbox' <%if ("true".equalsIgnoreCase ((String) modifyAccessMap.get (functionId+""))) out.print("checked");%> name='<%="FUNCTION_ID," + functionId + "," + groupId + ",modify"%>'> Modify &nbsp;&nbsp;&nbsp;&nbsp;
                <%}%>

                <%if (functionality.getDelete() == true) {%>
                    <input type='checkbox' <%if ("true".equalsIgnoreCase ((String) deleteAccessMap.get (functionId+""))) out.print("checked");%> name='<%="FUNCTION_ID," + functionId + "," + groupId + ",delete"%>'> Delete &nbsp;&nbsp;&nbsp;&nbsp;
                <%}%>

                </font></td>
            </tr>
<%
        }   // for (int j=0; j<permissionsSiz.....
%>
	</table>
</div>
</td>
</tr>

<%
    }   //for (int i=0; i<userGroupSi....
}   //if (userGroup != nu....
%>
</table>
<!-- GROUPS AND FUNCTIONALITY DISPLAY CODE ENDS HERE -->

</td></tr>
</table>

<!-- till here -->
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>

<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->

</html:form>

</body>

</html:html>
